package com.evaluation.util;

/**
 * @author bob.kuang
 *
 */

public class ServiceResult<T> {
	/** 成功 */
	public static final String SUCCESS = "0000";
	/** 应用异常 */
	public static final String APP_EXCEPTION = "9998";
	/** 其他异常 */
	public static final String FAIL = "9999";
	/** 没有登录 */
	public static final String NO_SESSION = "0001";

	private T result;

	private String msg;

	private String repCode = SUCCESS;

	/**
	 * 一个请求的调用链，唯一
	 */
	private String trackId;

	/**
	 * result 为列表时，存储总记录数
	 */
	private long total;
    
	public ServiceResult() {
		
	}

	public ServiceResult(String repCode, T result) {
		this.result = result;
		this.repCode = repCode;
	}

	public ServiceResult(String repCode) {
		this.repCode = repCode;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	public String getMsg() {
		return msg;
	}

	/**
	 * 失败时存储错误信息
	 * @param msg
	 */
	public ServiceResult<T> setMsg(String msg) {
		this.msg = msg;
		
		return this;
	}

	public String getRepCode() {
		return repCode;
	}

	public void setRepCode(String repCode) {
		this.repCode = repCode;
	}

	/**
	 * @return the total
	 */
	public long getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(long total) {
		this.total = total;
	}

	public void setSuccess() {
		this.repCode = SUCCESS;
	}

	public ServiceResult<T> setFail() {
		this.repCode = FAIL;
		return this;
	}

	public void setAppFail() {
		this.repCode = APP_EXCEPTION;
	}

	public String getTrackId() {
		return trackId;
	}

	public void setTrackId(String trackId) {
		this.trackId = trackId;
	}

	public void setAppFail(String message) {
		this.repCode = APP_EXCEPTION;
		this.msg = message;
	}

	public void setAppFail( String formatStr, Object... message) {
		this.repCode = APP_EXCEPTION;
		this.msg = String.format( formatStr, message );
	}
	
	public void setAppCodeFail( String resCode, String formatStr, Object... message) {
		this.repCode = resCode;
		this.msg = String.format( formatStr, message );
	}
	
	public boolean isSuccess() {
		return SUCCESS.equals(this.repCode);
	}

	/* 直接判断success即可 bob.kuang 20190529
	 * public boolean isFail() { return !SUCCESS.equals(this.repCode); }
	 */


	/**
	 * 成功 msg 提示
	 * @param msg
	 * @return
	 */
	public static ServiceResult<String> setResultSuccessMsg (String msg ) {
		ServiceResult<String> result=  new ServiceResult<>();
		result.setMsg(msg);
		result.setSuccess();
		return result;
	}

	public static <T> ServiceResult<T> setSuccessResult ( T t ) {
		ServiceResult<T> result=  new ServiceResult<>();
		result.setResult( t );
		result.setSuccess();
		return result;
	}
	
	/**
	 * 报错 msg 提示
	 * @param msg
	 * @return
	 */
	public static ServiceResult<String> setResultFailMsg (String msg) {
		ServiceResult<String> result=  new ServiceResult<>();
		result.setMsg(msg);
		result.setAppFail();
		return result;
	}

	@Override
	public String toString() {
		return "ServiceResult [result=" + result + ", msg=" + msg + ", repCode=" + repCode + ", trackId=" + trackId
				+ ", total=" + total + "]";
	}
	
}